//
//  PEPlantPowerCell.m
//  RuiHeAppApp
//
//  Created by Yan Chen on 2023/12/7.
//

#import "RHDevicePowerCell.h"
@interface RHDevicePowerCell()
@property (nonatomic, strong) QMUILabel *solarLab;

@property (nonatomic, strong) QMUILabel *loadLab;

@property (nonatomic, strong) QMUILabel *gridLab;

@property (nonatomic, strong) QMUILabel *batteryLab;

@end
@implementation RHDevicePowerCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if(self){
        [self setupUI];
    }
    return self;
}

-(void)setDic:(NSDictionary *)dic
{
    _dic = dic;
    self.solarLab.text = ValidNullDataToString(dic[@"solar"], @"--");
    self.gridLab.text = ValidNullDataToString(dic[@"grid"], @"--");
    self.batteryLab.text = ValidNullDataToString(dic[@"battery"], @"--");
    self.loadLab.text = ValidNullDataToString(dic[@"load"], @"--");
    
    self.solarLab.attributedText = [NSString attributeStringWithContent:self.solarLab.text attriFont:kSYSTEMFONT(10) attriColor:kCOLOR_HEX(@"#8B8D92") attriKey:dic[@"solar_unit"]];
    self.gridLab.attributedText = [NSString attributeStringWithContent:self.gridLab.text attriFont:kSYSTEMFONT(10) attriColor:kCOLOR_HEX(@"#8B8D92") attriKey:dic[@"grid_unit"]];
    self.batteryLab.attributedText = [NSString attributeStringWithContent:self.batteryLab.text attriFont:kSYSTEMFONT(10) attriColor:kCOLOR_HEX(@"#8B8D92") attriKey:dic[@"battery_unit"]];
    self.loadLab.attributedText = [NSString attributeStringWithContent:self.loadLab.text attriFont:kSYSTEMFONT(10) attriColor:kCOLOR_HEX(@"#8B8D92") attriKey:dic[@"load_unit"]];
}

- (void)setupUI{
    kWeakSelf;
    [self setBackgroundColor:CFTabViewBgColor];
    [self.contentView setBackgroundColor:CFTabViewBgColor];
    
    UIView *bgView = [[UIView alloc] init];
    bgView.layer.cornerRadius = 8;
    bgView.layer.masksToBounds = YES;
    [bgView setBackgroundColor:CFTabViewBgColor];
    [self.contentView addSubview:bgView];
    
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.insets(UIEdgeInsetsMake(0,0, 0, 0));
    }];
    
    __block UIView *lastView;
    __block CGFloat itemW = (kUI_WINDOW_W-30-10)/2;
    NSArray *titles = @[@"负载".rh_localized,@"光伏".rh_localized,@"电池".rh_localized,@"电网".rh_localized];
    NSArray *images = @[@"kwt_pow_load",@"kwt_pow_solar",@"kwt_pow_battery",@"kwt_pow_grid"];
    [titles enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        UIView *itemView = [weakSelf itemViewWithIndex:idx title:titles[idx] image:images[idx]];
        [itemView rounded:8 width:1 color:kBorderColor];
        [bgView addSubview:itemView];
        if(idx == 0){
            [itemView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.mas_offset(15);
                make.top.mas_offset(0);
                make.width.offset(itemW);
                make.height.offset(kScaleW(65));
            }];
        }else{
            if(idx%2==0){//换行
                [itemView mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.left.mas_offset(15);
                    make.top.equalTo(lastView.mas_bottom).offset(10);
                    make.size.equalTo(lastView);
                }];
            }else{
                [itemView mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.left.equalTo(lastView.mas_right).offset(10);
                    make.centerY.equalTo(lastView);
                    make.size.equalTo(lastView);
                }];
            }
        }
                
        lastView = itemView;
    }];
    
    //封底
    [lastView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_offset(0);
    }];
}

-(UIView*)itemViewWithIndex:(NSInteger)idx title:(NSString*)title image:(NSString*)imageStr
{
    UIView *bgView = kView;
    bgView.layer.cornerRadius = 8;
    bgView.layer.masksToBounds = YES;
    bgView.backgroundColor = UIColor.whiteColor;
    UIImageView *img = [[UIImageView alloc]initWithImage:[UIImage imageNamed:imageStr]];
    [bgView addSubview:img];
    [img mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.offset(25);
        make.left.mas_offset(15);
        make.centerY.mas_offset(0);
        
    }];
    
    QMUILabel *titleLab = [[QMUILabel alloc]init];
    titleLab.textAlignment = NSTextAlignmentLeft;
    titleLab.numberOfLines = 2;
    titleLab.adjustsFontSizeToFitWidth = YES;
    titleLab.minimumScaleFactor = 0.6;
    titleLab.text = [NSString stringWithFormat:@"%@",title];
    titleLab.textColor = [UIColor colorWithHexString:@"3F4443" alpha:1];
    titleLab.font = [UIFont systemFontOfSize:14];
    [bgView addSubview:titleLab];
    
    QMUILabel *contentLab = [[QMUILabel alloc]init];
    contentLab.textAlignment = NSTextAlignmentLeft;
    contentLab.tag = 200 + idx;
    contentLab.text = @"--";
    contentLab.textColor = [UIColor colorWithHexString:@"3F4443" alpha:1];
    contentLab.font = [UIFont boldSystemFontOfSize:16];
    [bgView addSubview:contentLab];
    
    [titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(img.mas_right).offset(12);
        make.right.mas_offset(-3);
        make.centerY.equalTo(img.mas_top);
    }];
    
    [contentLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(titleLab);
        make.top.equalTo(titleLab.mas_bottom).mas_offset(5);
    }];
    
   
    if (idx == 0) {
        self.loadLab = contentLab;
    }
    if (idx == 1) {
        self.solarLab = contentLab;
    }
    if (idx == 2) {
        self.batteryLab = contentLab;
    }
    if (idx == 3) {
        self.gridLab = contentLab;
    }
    return bgView;
}

@end
